<template>
  <div class="json-viewer-box">
    <span>
      <div class="jv-value box" @click="setOpen">
        {{JSON.stringify(value)}}
      </div>
      <div class="close-box">
        <span class="close" @click="copyEvent(value)">复制文本</span>
      </div>
      <span class="jv-toggle" :class="{open: isOpen}"></span>
      <template v-if="!isOpen">
        <span class="jv-item jv-object">
          {
        </span>
        <span @click="setOpen" class="jv-ellipsis">...</span>
        <span @click="setOpen" class="jv-item jv-object">
          }
        </span>
      </template>
      <template v-else>
        <span class="jv-item jv-object">
          {
        </span>
        <div v-for="(val, key) in value" class="jv-node" :key="key">
          <span class="jv-key">{{key}}:</span>
          <span class="jv-value" v-if="typeof val === 'string'">"{{val}}"</span>
          <span class="jv-number" v-if="typeof val === 'number'">{{val}}</span>
        </div>
        <span class="jv-item jv-object">
          }
        </span>
      </template>
    </span>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'

export default defineComponent({
  name: 'Home',
  components: {
  },
  props: {
    value: {
      type: Object,
      default: () => null
    }
  },
  setup () {
    const isOpen = ref(false)
    const setOpen = () => {
      isOpen.value = !isOpen.value
    }
    const copyEvent = val => {
      if (typeof val !== 'string') {
        val = JSON.stringify(val)
      }
      const span:HTMLSpanElement = document.createElement('span')
      span.innerText = val
      span.style.top = '-100px'
      span.style.position = 'fixed'
      document.body.appendChild(span)
      if (window.getSelection) {
        const Selection:any = window.getSelection()
        Selection.selectAllChildren(span)
        const success = document.execCommand('Copy')
        if (success) {
          (document.activeElement as any).blur()
          const selection:any = window.getSelection()
          selection.removeAllRanges()
        }
        span.remove && span.remove()
      }
    }
    return {
      isOpen,
      setOpen,
      copyEvent
    }
  }
})
</script>
<style lang="less" scoped>
.json-viewer-box{
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 6px;
  margin: 10px 0px;
  .box{
    cursor: pointer;
    -moz-user-select: none;
    -o-user-select:none;
    -khtml-user-select:none;
    -webkit-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
  .close-box{
    cursor: pointer;
    span.close{
      &:hover{
        opacity: 0.7;
      }
      display: inline-block;
      height: 24px;
      margin: 10px 0px;
      padding: 0 5px;
      font-size: 12px;
      border-radius: 2px;
      line-height: 24px;
      text-align: center;
      color: #fff;
      border-color: #42b983;
      background: #42b983;
      box-shadow: 0 2px #0000000b;
      transition: all .3s;
      cursor: pointer;
    }
  }
  &:hover{
    box-shadow: 0 2px 7px rgb(0, 0, 0, 15%);
    border-color: transparent;
  }
  span.jv-toggle{
    &.open{
      transform: rotate(90deg);
    }
    background-image: url();
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center center;
    width: 10px;
    height: 10px;
    margin-right: 2px;
    display: inline-block;
    transition: transform 0.1s;
  }
  span.jv-item{
    background: #fff;
    white-space: nowrap;
    color: #525252;
    font-size: 14px;
    font-family: Consolas, Menlo, Courier, monospace;
  }
  span.jv-object{
    color: #111111;
  }
  span.jv-ellipsis{
    color: #999;
    background-color: #eee;
    display: inline-block;
    line-height: 0.9;
    font-size: 0.9em;
    padding: 0px 4px 2px 4px;
    margin: 0 4px;
    border-radius: 3px;
    vertical-align: 2px;
    cursor: pointer;
    user-select: none;
  }
  .jv-node{
    margin-left: 25px;
  }
  .jv-key{
    color: #111111;
    margin-right: 4px;
    font-size: 12px;
    font-family: Consolas, Menlo, Courier, monospace;
  }
  .jv-value{
    color: #42b983;
    word-break: break-word;
    font-size: 12px;
    font-family: Consolas, Menlo, Courier, monospace;
  }
  .jv-number{
    color: #fc1e70;
    font-size: 12px;
    font-family: Consolas, Menlo, Courier, monospace;
  }
}
</style>
